#! /usr/bin/python
import argparse
import sys
import random

from libs.utils import color, put, on
from libs.HueScript import HueScript, TT_SEC

class RandomColorScript(HueScript):
    def __init__(self, *posargs, **kwargs):
        HueScript.__init__(self, *posargs, **kwargs)

    def _setUpArgParser(self):
        HueScript._setUpArgParser(self)

        self.parser.add_argument('--brightness', '-b', default=200, type=int,
                help='Brightness value (0-255).  Superceded by --randomBrightness.', metavar='BRI')
        self.parser.add_argument('--randomBrightness', '-B', action='store_true',
                help='Random brightness.  Supercedes --brightness.')
        self.parser.add_argument('--transitiontime', '-t', default=20, type=int, 
                help='Length of cue time (in seconds).', metavar='SECS')
        self.parser.add_argument('--lamps', '-l', nargs='*', default=[1, 2, 3], type=int,
                help='Numeric IDs of lamps to affect.', metavar='ID')
        self.parser.add_argument('--hold', '-H', default=0, type=int,
                help='Time between transitions (in seconds).', metavar='SECS')
        self.parser.add_argument('numCues', default=30, metavar='numCues', type=int,
                help='Number of cues to generate.')

    def _parseArgs(self):
        HueScript._parseArgs(self)

    def generateCues(self):
        self.register(0, on, [self.args.lamps])

        tt = self.args.transitiontime
        hold = self.args.hold
        bri = self.args.brightness

        for i in range(0, self.args.numCues):
            for l in self.args.lamps:
                r = random.randrange(0, 255)
                g = random.randrange(0, 255)
                b = random.randrange(0, 255)

                if self.args.randomBrightness:
                    bri = random.randrange(0, 255)

                script.register( (i*tt) + hold, color, [[l], r, g, b, {
                                    'bri': bri, 
                                    'transitiontime': tt*TT_SEC
                                }],
                            relative=False)

        self.writeLog(self.getCueListStr())

if __name__ == '__main__':
    script = RandomColorScript()
    script.generateCues()
    script.execute()
